package com.sina.shop.api;

import com.sina.shop.core.exception.serviceexception.Error;
import com.sina.shop.core.exception.serviceexception.ResourceNotFoundException;
import com.sina.shop.domain.log.Log;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-01-31-14:49
 */
@RequestMapping("/log")
@RestController
public class LogController extends BaseController {


    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getLog", method = RequestMethod.GET)
    public Log getLog(@RequestParam(value = "id") Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getLog, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        //Carousel carousel = carouselService.findOne(id);
        return new Log();
    }

    @RequestMapping(value = "/saveLog", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public Log saveLog(Log log) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveLog, method: post, host: " + instance.getHost() + ", serviceId: " + instance.getServiceId() + ",time: " + LocalDateTime.now());

        Log saved = new Log();//carouselService.save(carousel);

        return saved;
    }

    @RequestMapping(value = "/deleteLog", method = RequestMethod.POST)
    public int deleteLog(@RequestParam(value = "id") Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteLog, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        //carouselService.delete(id);
        return 1;
    }

    @RequestMapping(value = "/getLogList", method = RequestMethod.GET)
    public List<Log> getLogList(Log log) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getLogList, get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        // List<Carousel> carousels = carouselService.findByUsedFor(usedFor);
        List<Log> list = new ArrayList<>();
        list.add(new Log());
        list.add(new Log());
        return list;
    }

    @ExceptionHandler(ResourceNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Error resourceNotFound(ResourceNotFoundException e) {
        String resourceId = e.getResourceId();
        return new Error(4, "Log [" + resourceId + "] not found");
    }
}
